//
//  CZEmoticonToolBar.swift
//  007-表情键盘
//
//  Created by 名品导购网MPLife.com on 2017/12/15.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit

@objc protocol CZEmoticonToolBarDlegate : NSObjectProtocol {
    /// 表情工具栏选中项目分组项索引
    ///
    /// - Parameters:
    ///   - toolBar: 工具栏
    ///   - index: 索引
    func emoticonToolBarDidSelectedItemIndex(toolBar:CZEmoticonToolBar,index:Int)
}

//表情键盘 底部工具栏
class CZEmoticonToolBar: UIView {
    
    weak var delegate:CZEmoticonToolBarDlegate?
    var  selectedIndex : Int = 0 {
        didSet{
            //1.取消所有的选中状态
            for btn in subviews as![UIButton] {
                btn.isSelected = false
            }
            //设置index对应的选中状态为true
            (subviews[selectedIndex] as! UIButton).isSelected = true
        }
    }
    
    override func awakeFromNib() {
        setUPUI()
    }
    
    override func layoutSubviews() {
        //布局所有按钮
        let  count = subviews.count
        let w = bounds.width/CGFloat(count)
        let rect = CGRect(x: 0, y: 0, width: w, height: bounds.height)
        
        for (i,btn) in subviews.enumerated() {
            btn.frame = rect.offsetBy(dx: CGFloat(i)*w, dy: 0)
        }
    }
    //点击分组项按钮
    @objc private func clickItem(button:UIButton){
        //通知代理执行协议方法
        delegate?.emoticonToolBarDidSelectedItemIndex(toolBar: self, index: button.tag)
    }
}
private extension CZEmoticonToolBar{

    func setUPUI() {
        //设置按钮
        //从表情包的分组名称设置按钮
        let manager = CZEmoticonManager.shared
        for (i,p) in manager.packages.enumerated() {
            let btn = UIButton()
            btn .setTitle(p.groupName, for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn .setTitleColor(UIColor.white, for: .normal)
            btn.setTitleColor(UIColor.darkGray, for: .highlighted)
            btn.setTitleColor(UIColor.darkGray, for: .selected)
            btn.tag = i;
            btn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
            //设置按钮图片
            let imageName = "compose_emotion_table_\(String(describing: p.bgImageName))_normal"
            let imageNameHL = "compose_emotion_table_\(String(describing: p.bgImageName))_selected"
            var image = UIImage(named: imageName, in: manager.bundle, compatibleWith: nil)
            //拉伸图像
            let size = image?.size ?? CGSize()
            
            let inset = UIEdgeInsets(top: size.height*0.5, left: size.width * 0.5, bottom: size.height*0.5, right: size.width*0.5)
            image = image?.resizableImage(withCapInsets: inset)
            var imageHL = UIImage(named: imageNameHL, in: manager.bundle, compatibleWith: nil)
            imageHL = imageHL?.resizableImage(withCapInsets: inset)
            btn .setBackgroundImage(image, for: .normal)
            btn .setBackgroundImage(imageHL, for: .highlighted)
            btn .setBackgroundImage(imageHL, for: .highlighted)
            btn.sizeToFit()
            addSubview(btn)
        }
        
        //默认选中第0个按钮
        (subviews[0] as! UIButton) .isSelected = true
        
    }
}






